package org.ljy.rabbitextensionsdemo.listener;

import com.rabbitmq.client.Channel;
import org.ljy.rabbitextensionsdemo.constant.Constant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RetryListener {

    //自动确认的消费者
    //@RabbitListener(queues = Constant.RETRY_QUEUE)
    public void handleMessage(Message message) throws Exception {
        System.out.println("消费消息："+new String(message.getBody()));
        System.out.println("异常前");
        int a=9/0;
        System.out.println("异常后");
    }

    //自动确认的消费者
    @RabbitListener(queues = Constant.RETRY_QUEUE)
    public void handlerMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("["+Constant.RETRY_QUEUE+"]接收到消息: %s, deliveryTag: %s \n", new String(message.getBody(), "UTF-8"), deliveryTag);
        try {
            int num = 3/0;
            System.out.println("业务处理完成");
            channel.basicAck(deliveryTag, false);
        }catch (Exception e){
            channel.basicNack(deliveryTag, false, false);
        }
    }

}
